package com.teradata.tool.ui;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import com.teradata.tool.engine.SqlToPerl;
import com.teradata.tool.xml.bean.SqlToPerlBean;
import com.teradata.tool.xml.util.XmlUtil;

public class SqlToPerlUi extends JDialog {

	/**
	 * 
	 */
	private static final long	serialVersionUID	= 7260622353100055919L;
	private final JPanel		contentPanel		= new JPanel();
	private JTextField			sourceDirTextField;
	private JTextField			sourceFileTextField;
	private JTextField			templateFileTextField;
	private JTextField			targetDirTextField;
	private JTextField			targetFileTextField;
	private JTextField			replaceStringTextField;

	private final SqlToPerlBean	sqlToPerlBean;
	private final JFileChooser	fc					= new JFileChooser();
	private final XmlUtil		sqlToPerlBeanUtil;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		try {
			SqlToPerlUi dialog = new SqlToPerlUi();
			dialog.setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);
			dialog.setVisible(true);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * Create the dialog.
	 */
	public SqlToPerlUi() {
		setBounds(100, 100, 561, 342);
		getContentPane().setLayout(new BorderLayout());
		contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
		getContentPane().add(contentPanel, BorderLayout.CENTER);
		contentPanel.setLayout(null);

		sqlToPerlBean = new SqlToPerlBean();
		sqlToPerlBeanUtil = new XmlUtil(sqlToPerlBean);
		sqlToPerlBeanUtil.getIndexOfBean(0);
		System.out.println("getPerlFileDir:" + sqlToPerlBean.getPerlFileDir());
		{
			JLabel sourceDirLabel = new JLabel("\u6765\u6E90\u76EE\u5F55:");
			sourceDirLabel.setBounds(10, 11, 67, 14);
			contentPanel.add(sourceDirLabel);
		}

		sourceDirTextField = new JTextField();
		sourceDirTextField.setBounds(68, 8, 347, 20);
		sourceDirTextField.setText(sqlToPerlBean.getSqlDir());
		contentPanel.add(sourceDirTextField);
		sourceDirTextField.setColumns(10);

		JButton sourceDirButton = new JButton("\u6D4F\u89C8");
		sourceDirButton.setBounds(434, 7, 89, 23);
		sourceDirButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				lookUp("sourceDirButton");
			}
		});
		contentPanel.add(sourceDirButton);

		JLabel sourceFileLabel = new JLabel("\u6765\u6E90\u6587\u4EF6:");
		sourceFileLabel.setBounds(10, 36, 56, 14);
		contentPanel.add(sourceFileLabel);

		sourceFileTextField = new JTextField();
		sourceFileTextField.setBounds(68, 33, 347, 20);
		sourceFileTextField.setText(sqlToPerlBean.getSqlFileName());
		contentPanel.add(sourceFileTextField);
		sourceFileTextField.setColumns(10);

		JLabel templateLabel = new JLabel("\u6A21\u677F\u6587\u4EF6:");
		templateLabel.setBounds(10, 61, 56, 14);
		contentPanel.add(templateLabel);

		templateFileTextField = new JTextField();
		templateFileTextField.setBounds(68, 58, 347, 20);
		templateFileTextField.setText(sqlToPerlBean.getTemplatePerlFile());
		contentPanel.add(templateFileTextField);
		templateFileTextField.setColumns(10);

		JButton templateFileButton = new JButton("\u6D4F\u89C8");
		templateFileButton.setBounds(434, 57, 89, 23);
		templateFileButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				lookUp("templateFileButton");
			}
		});
		contentPanel.add(templateFileButton);

		JLabel targetDirLabel = new JLabel("\u76EE\u6807\u76EE\u5F55:");
		targetDirLabel.setBounds(10, 86, 67, 14);
		contentPanel.add(targetDirLabel);

		targetDirTextField = new JTextField();
		targetDirTextField.setBounds(67, 83, 348, 20);
		targetDirTextField.setText(sqlToPerlBean.getPerlFileDir());
		contentPanel.add(targetDirTextField);
		targetDirTextField.setColumns(10);

		JButton targetDirButton = new JButton("\u6D4F\u89C8");
		targetDirButton.setBounds(434, 82, 89, 23);
		targetDirButton.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				lookUp("targetDirButton");
			}
		});
		contentPanel.add(targetDirButton);

		JLabel templateFileLabel = new JLabel("\u76EE\u6807\u6587\u4EF6:");
		templateFileLabel.setBounds(10, 114, 56, 14);
		contentPanel.add(templateFileLabel);

		targetFileTextField = new JTextField();
		targetFileTextField.setBounds(68, 108, 347, 20);
		targetFileTextField.setText(sqlToPerlBean.getPerlFileName());
		contentPanel.add(targetFileTextField);
		targetFileTextField.setColumns(10);

		JLabel replateStringLabel = new JLabel("\u66FF\u6362\u8BED\u53E5:");
		replateStringLabel.setBounds(10, 139, 56, 14);
		contentPanel.add(replateStringLabel);

		replaceStringTextField = new JTextField();
		replaceStringTextField.setBounds(68, 136, 347, 20);
		replaceStringTextField.setText(sqlToPerlBean.getTemplateString());
		contentPanel.add(replaceStringTextField);
		replaceStringTextField.setColumns(10);
		{
			JPanel buttonPane = new JPanel();
			buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
			getContentPane().add(buttonPane, BorderLayout.SOUTH);
			{
				final JButton okButton = new JButton("\u786E\u5B9A");
				okButton.setActionCommand("OK");
				okButton.addActionListener(new ActionListener() {

					@Override
					public void actionPerformed(ActionEvent e) {
						okButton.setVisible(true);
						//
						sqlToPerlBean.setSqlDir(sourceDirTextField.getText());
						sqlToPerlBean.setPerlFileDir(targetDirTextField.getText());
						sqlToPerlBean.setTemplatePerlFile(templateFileTextField.getText());
						sqlToPerlBean.setPerlFileName(targetFileTextField.getText());
						sqlToPerlBean.setSqlFileName(sourceFileTextField.getText());
						sqlToPerlBean.setTemplateString(replaceStringTextField.getText());

						sqlToPerlBeanUtil.modifyMsg();
						SqlToPerl sqlToPerl = new SqlToPerl(sqlToPerlBean.getSqlDir(), sqlToPerlBean.getSqlFileName(),
								sqlToPerlBean.getPerlFileName(), sqlToPerlBean.getTemplatePerlFile(), sqlToPerlBean
										.getTemplateString(), sqlToPerlBean.getPerlFileDir());
						try {
							sqlToPerl.run();
						} catch (Exception e1) {
							// TODO Auto-generated catch block
							e1.printStackTrace();
						}

					}
				});
				buttonPane.add(okButton);
				getRootPane().setDefaultButton(okButton);
			}
			{
				JButton cancelButton = new JButton("Cancel");
				cancelButton.setActionCommand("Cancel");
				buttonPane.add(cancelButton);
			}
		}
	}

	private void lookUp(String buttonName) {
		// tFpath, tFfpath, tFmpath, tFf1model, tFf1mode2, tFspath, tFchange;
		if (buttonName.equals("sourceDirButton")) {
			fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
			int intRetVal = fc.showOpenDialog(this);
			if (intRetVal == JFileChooser.APPROVE_OPTION) {
				sourceDirTextField.setText(fc.getSelectedFile().getPath().replace("\\", "/"));
			}
		}
		if (buttonName.equals("templateFileButton")) {
			fc.setFileSelectionMode(JFileChooser.FILES_ONLY);
			int intRetVal1 = fc.showOpenDialog(this);
			if (intRetVal1 == JFileChooser.APPROVE_OPTION) {
				templateFileTextField.setText(fc.getSelectedFile().getPath().replace("\\", "/"));
			}
		}
		if (buttonName.equals("targetDirButton")) {
			fc.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
			int intRetVal1 = fc.showOpenDialog(this);
			if (intRetVal1 == JFileChooser.APPROVE_OPTION) {
				targetDirTextField.setText(fc.getSelectedFile().getPath().replace("\\", "/"));
			}
		}
	}
}
